//
//  SearchFirendVC.m
//  tcpoctest
//
//  Created by gump on 11/5/12.
//  Copyright (c) 2012 gump. All rights reserved.
//

#import "SearchFirendVC.h"
#import "WFLabel_blackShadow.h"
#import "PublicFun.h"
#import "netManager.h"
#import "DataBaseCtrol.h"
#import "ViewManager.h"
#import "FTWButton.h"

@interface SearchFirendVC ()

@end

@implementation SearchFirendVC

- (id)init
{
    self = [super init];
    if (self) {
        isSearching = false;
        iWaitingTimer = 0;
    }
    return self;
}

-(void)initUI
{
    self.view.backgroundColor = VIEW_BG_COLOR;
    
    [self createTitle];
    self.mtitle.text = @"找朋友";
    
    UIBarButtonItem *backItem = [self createBarBackButtonItemWithTitle:@"返回" target:self action:@selector(backButtonPressed)];
    self.navItem.leftBarButtonItem = backItem;
    
    UIImageView *inputBgView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 70, 300, 44)];
    inputBgView.image = [UIImage imageNamed:@"cell.png"];
    inputBgView.userInteractionEnabled = YES;
    [self.view addSubview:inputBgView];
    //[inputBgView release];
    
    inputTF = [[UITextField alloc]initWithFrame:CGRectMake(5, 0, 290, 44)];
    inputTF.font = CENTER_CELL_CONTENT_FONT;
    inputTF.textColor=[UIColor colorWithWhite:0.4 alpha:1.0];
    inputTF.borderStyle=UITextBorderStyleNone;
    inputTF.leftViewMode = UITextFieldViewModeAlways;
    inputTF.placeholder = [NSString stringWithFormat:@"请输入%@号",T_PROJECTNAME];
    inputTF.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    inputTF.delegate =self;
    inputTF.keyboardType = UIKeyboardTypeNumberPad;
    [inputBgView addSubview:inputTF];
    //[inputTF release];
    
    FTWButton *logout = [[FTWButton alloc] init];
    logout.frame = CGRectMake(7, 150, 306, 44);
    [logout addBlueStyleForState:UIControlStateNormal];
    [logout setText:@"查找" forControlState:UIControlStateNormal];
    [logout setFont:[UIFont systemFontOfSize:18]];
    [logout addTarget:self action:@selector(searchButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:logout];
}

#pragma mark -private fun-
-(void)searchButtonPressed
{
    [inputTF resignFirstResponder];
    
    if ([inputTF.text length] != 0)
    {
        if([inputTF.text integerValue] == 
                [DataControl shareControl].personDS.uinfo.ppNumber)
        {
            [[ViewManager sharedViewManager] showView:VI_USERINFO
                                     withIsRemoveCurV:true
                                        withDirection:VD_RIGHT];
        }
        else
        {
            
            NSInteger pnum = [inputTF.text intValue];
            [[DataControl shareControl] setSearchPPNum:pnum];
            [[DataControl shareControl].LCDS addLogicCommand:LC_SEARCHFRIEND];
            isSearching = true;
            
            [inputTF resignFirstResponder];
            
            [[ViewManager sharedViewManager] showSuccessProgressHUDWithStatus:@"搜索好友"
                                                                     withView:self.view];
        }
        
        
    }
    else
    {
        [[ViewManager sharedViewManager] showFailHUDWithStatus:
         [NSString stringWithFormat:@"%@号不可为空",T_PROJECTNAME]
                                                     withDelay:1
                                                      withView:self.view];
    }
}

-(void)searchFail
{
    if(isSearching)
    {
        iWaitingTimer = 0;
        isSearching = false;
        
        [[ViewManager sharedViewManager] showFailHUDWithStatus:@"无此用户"
                                                     withDelay:1
                                                      withView:self.view];
    }
}

-(void)backButtonPressed
{
    [[ViewManager sharedViewManager] backView:self];
}

-(void)UICustomTick
{
    if(isSearching)
    {
        if(iWaitingTimer > WAIT_TIMMER)
        {
            iWaitingTimer = 0;
            isSearching = false;
            
            [[ViewManager sharedViewManager] dismissProgressHUD];
        }
        else
        {
            ++iWaitingTimer;
        }
    }
}

-(void)processSearchFirend
{
      if(isSearching)
      {
          iWaitingTimer = 0;
          isSearching = false;
          
          [[ViewManager sharedViewManager] dismissProgressHUD];
          
          [[ViewManager sharedViewManager] showView:VI_PERSONINFO
                                   withIsRemoveCurV:false
           withDirection:VD_RIGHT];
      }
}

-(void)viewWillHide
{
    iWaitingTimer = 0;
    isSearching = false;
    inputTF.text = @"";
    
    [[ViewManager sharedViewManager] dismissProgressHUD];
}

#pragma mark -textfield delegate-
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return true;
}
@end
